package com.bmt.pos.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.bmt.core.BaseContoller;
import com.bmt.core.ISaveable;
import com.bmt.core.PageListInfo;
import com.bmt.exception.ActionException;
import com.bmt.exception.ServiceException;
import com.bmt.pos.bean.CodeTable;
import com.bmt.pos.bean.Store;
import com.bmt.pos.service.StoreService;
import com.bmt.utils.String_Utils;
import com.google.gson.Gson;

@Controller
@RequestMapping(value = "storemgr")
public class StoreContoller extends BaseContoller {

	@Autowired
	private StoreService storeService;
	
	@RequestMapping(value = "get/{id}.htm", method = RequestMethod.GET)
	public ModelAndView getStoreInfo(@PathVariable Integer id) throws ActionException {
		System.out.println("getStoreInfo====="+id);
		if(id==null){
			throw new ActionException("Store Id is null,can not find the Store Infomation");
		}
		
		Store store=storeService.getStoreById(id);
		if(store==null){
			//跳转到找不到资源的页面
			throw new ActionException("Store is null,can not find the Store Infomation");
		}
		System.out.println();
		ModelAndView mv = new ModelAndView("store_view");
		mv.addObject("store", store);
		return mv;
	}
	
	@RequestMapping(value = "storeList.htm", method = RequestMethod.GET)
	public ModelAndView list() throws ServiceException {
		int pageNum=1;
		ModelAndView mv = new ModelAndView("store_list");
		PageListInfo<ISaveable> plInfo = storeService.getPageInfo(pageNum);
		mv.addObject("plInfo", plInfo);
		
		return mv;
		
	}
	
	@RequestMapping(value = "page/{pageNum}.htm", method = RequestMethod.GET)
	public ModelAndView getByPaging(@PathVariable Integer pageNum) throws Exception {
		ModelAndView mv = new ModelAndView("store_list");
		PageListInfo<ISaveable> plInfo = storeService.getPageInfo(pageNum);
		mv.addObject("plInfo", plInfo);
		return mv;
	}
	
	@RequestMapping(value = "saveStore.htm", method = RequestMethod.POST)
	public ModelAndView saveStore(@ModelAttribute("form")Store store,HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//List<Store> storeList = storeService.getStoreList();
		String result=null;
		PrintWriter pw = null;
		boolean isSuccess=false;
		System.out.println("createStore=====> code :" +store.getCode());
		store.setActive(true);
		if(store.getId()!=null && !"".equals(store.getId().toString().trim()) && NumberUtils.isNumber(store.getId().toString().trim())){
			//保存修改
			isSuccess=storeService.updateStore(store);
		}else {
			//新建
			isSuccess=storeService.createStore(store);
		}
		
		if(isSuccess){
			result="success";
			boolean flag=true;
			Gson gson=new Gson();
			String json=gson.toJson(flag);
			pw = resp.getWriter();
			pw.write(json);
			pw.flush();
			req.setAttribute("result", "success");
			return null;
		}else {
			result="fail";
			return new ModelAndView("store_detail", "msg", result);
		}
		
	}
	
	@RequestMapping(value = "storeDetail.htm")
	public ModelAndView storeDetail(HttpServletRequest req, HttpServletResponse resp) throws ServiceException {
		//List<Store> storeList = storeService.getStoreList();
		ModelAndView mv=new ModelAndView("store_detail");
		String strId=req.getParameter("id");
		
		Store store=null;
		if(!String_Utils.isNullOrEmpty(strId)){
			store=storeService.getStoreById(Integer.parseInt(strId));
			mv.addObject("store", store);
		}
		
		List<CodeTable> oswList=storeService.getOSWList();
		System.out.println("=====storeDetail.htm======");
		
		mv.addObject("oswList", oswList);
		return mv;
	}
	
	


}
